Skip to content

Improve return type of array_splice() #3947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

herndlm
Copy link
Contributor

@herndlm herndlm commented Apr 18, 2025

Since array_splice returns an extract, similar as array_slice, we can make use of what we built already for the latter and get better return types with minimal effort.

https://www.php.net/manual/en/function.array-splice.php

https://3v4l.org/ggnQB

@herndlm
Copy link
Contributor Author

herndlm commented Apr 18, 2025

This also should fix @Seldaek's example mentioned in phpstan/phpstan#11917 (comment), but the original issue was dealing with the inferred type of the passed array, so we can't close that yet. For that I have an idea (Type::spliceArray() similar as we also did with e.g. array_pop and array_shift ) but it's out of scope of this PR and will have to deal with many edge cases and such.

See https://github.com/phpstan/phpstan-src/actions/runs/14536946202

@herndlm
Copy link
Contributor Author

herndlm commented Apr 18, 2025

also: there's a small change that test results change after merging #3946. therefore rebasing this one afterwards would be a good idea :)
but there aren't many array_splice() tests yet apparently.

@ondrejmirtes ondrejmirtes merged commit 0d9c974 into phpstan:2.1.x Apr 19, 2025
417 of 418 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@herndlm herndlm deleted the improve-array-splice-return-type branch April 19, 2025 12:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants